package ece1779.servlets;

import java.io.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.sql.DataSource;

public class SessionExample extends HttpServlet {
    public void doGet(HttpServletRequest request,
	              HttpServletResponse response)
    throws IOException, ServletException
    {
    	Connection con = null;
 
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
	
        HttpSession session = request.getSession();
        String  loggedIn = (String)session.getAttribute("loggedIn");
        //Integer numTimes = (Integer)session.getAttribute("numTimes");

        String login = null;
        String password = null;
        int userId = 0;
        int exist = 0;
        String actualpw = null;
      
        //int nTimes = 1;

	//if (numTimes != null)
	    //nTimes = numTimes.intValue() + 1;

        if (loggedIn == null) {
            login = request.getParameter("login");
            password = request.getParameter("password");
            if( login != null ){
	           	try{
		            // Get DB connection from pool
				    DataSource dbcp = (DataSource)this.getServletContext().getAttribute("dbpool");
				    con = dbcp.getConnection();
			
				    // Execute SQL query
				    Statement stmt = con.createStatement();
				    
				    String select = "SELECT * FROM users WHERE login ='" + login +"' ;";
				    ResultSet rs = stmt.executeQuery(select);
				    while(rs.next()){
				    	exist = 1;
				    	actualpw = rs.getString("password");
				    	userId = rs.getInt("id");
				    }
				    
				    if (exist != 0 && 
				        password != null && password.compareTo(actualpw) == 0) {
				        loggedIn = "true";
				    }
	           	}catch(Exception ex) {
					  //out.println(ex.getMessage());
			          getServletContext().log(ex.getMessage());  
				}    	
				finally {
					try {
					con.close();
					} 
					catch (Exception e) {
			            getServletContext().log(e.getMessage());  
					}
				}  
            }
        }
        
	out.println("<head><title>Simple Session Example</title></head>");
	out.println("<body>");
	out.println("<h1>Image Transform Login</h1>");

	if (loggedIn != null) {
            session.setAttribute("loggedIn", loggedIn);
            out.println("<b>User Menu</b>");     
            out.println("<ul><li><a href='ViewImages'>View Images</a></li>");   
            out.println("<li><a href='SessionLogout'>Logout</a></li></ul>");      
	}
	else {
	    if (login != null)
		out.println("Login failed!  Try again. <br>");
	    out.println("<form action='LoginWithJsp'> ");//'SessionExample'> ");
	    out.println("Login <input type='text' name='login' />");
	    out.println("Password <input type='text' name='password' />");
	    out.println("<input type='submit' />");
	    out.println("</form>");
	    out.println("<p>Don't have an account? Sign up <a href=\"/ece1779/servlet/CreateUser\">here</a>.");

	}
        out.println("</body>");
        out.println("</html>");
    }
    	
 	
    
}
